#include <allegro.h>
#include <ctime>    // For time()
#include <cstdlib>  // For srand() and rand()
#include <iostream>

using namespace std;

#include "graphics.h"
#include "layers.h"

// Timer setup
volatile int gametime = 0, fps_timer = 0;
void Timer() { ++gametime; ++fps_timer; } END_OF_FUNCTION(Timer);

int main()
{
	// standard initialization
	allegro_init();
	install_keyboard();
	
	// run the configuration stuffs
	set_config_file("config.txt");
	
	// set graphics mode
	set_color_depth(get_config_int("Initialization", "colordepth",   16));
	if(set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) < 0) return 0;
	
	// usual timer setup
	LOCK_VARIABLE(game_time);
	LOCK_VARIABLE(fps);
	LOCK_FUNCTION((void*)Timer);
	install_int_ex(Timer, BPS_TO_TIMER(240));
	
	// initialize the screen update method
	initialize_screen_updating(get_config_int("Initialization", "updatemethod", UPDATE_TRIPLE_BUFFER));

	// create the player
	CPlayer MyPlayer;
	

	// misc. variables for use below
	int fps = 0, num_frames_drawn = 0;
	bool update_gfx = 0;
	bool switchkey = false, vsynckey = false, showtext = false;
		
	Layer test;
	test = layerTest(100);
	
	MyPlayer.sprite = test.layer;
		
	// start the main game loop
	while(1)
	{
		if(update_gfx)
		{
			// fps code
			num_frames_drawn++;
			if(fps_timer >= 240)
			{
				fps = (240 * num_frames_drawn) / fps_timer;
				num_frames_drawn = fps_timer = 0;
			}
			
			// start the drawing
			clear_bitmap(get_buffer());
			MyPlayer.Draw(get_buffer());
			
			//if (showtext == true){ // para mostrar o no el tipo de update
			textprintf_ex(get_buffer(), font, 5, 10, makecol(255, 255, 255),-1, "fps: %i",  fps);
				switch(get_update_method())
				{
					case UPDATE_TRIPLE_WMB:    textout_ex(get_buffer(), font, "Triple Buffering with Buffering", 5, 20, makecol(255, 255, 255), -1); break;
					case UPDATE_PAGEFLIP_WMB:  textout_ex(get_buffer(), font, "Page Flipping with Buffering",    5, 20, makecol(255, 255, 255), -1); break;
					case UPDATE_TRIPLE_BUFFER: textout_ex(get_buffer(), font, "Triple Buffering", 5, 20, makecol(255, 255, 255), -1); break;
					case UPDATE_PAGE_FLIP:     textout_ex(get_buffer(), font, "Page Flipping",    5, 20, makecol(255, 255, 255), -1); break;
					case UPDATE_SYSTEM_BUFFER: textout_ex(get_buffer(), font, "System Buffering", 5, 20, makecol(255, 255, 255), -1); break;
					case UPDATE_DOUBLE_BUFFER: textout_ex(get_buffer(), font, "Double Buffering", 5, 20, makecol(255, 255, 255), -1); break;
				}
			
			if(vsync_is_enabled())
				textout_ex(get_buffer(), font, "VSync Enabled", 5, 30, makecol(255, 255, 255), -1);
			else
				textout_ex(get_buffer(), font, "VSync Disabled", 5, 30, makecol(255, 255, 255), -1);
			
			if(is_video_bitmap(get_buffer()))
				textout_ex(get_buffer(), font, "Video Buffer", 5, 40, makecol(255, 255, 255), -1);
			else
				textout_ex(get_buffer(), font, "Memory Buffer", 5, 40, makecol(255, 255, 255), -1);
			
			//} // para mostrar o no el tipo de update
				
			// end the drawing
			update_screen();
			update_gfx = 0;
			rest(10); // Para evitar el uso del 100% del cpu [deberia ser un valor dinamico]
		}
		
		while(gametime > 0)
		{
			// if the escape key is pressed, shutdown and exit
			if(key[KEY_ESC] || key[KEY_ENTER])
			{
				set_config_int("Initialization", "updatemethod", get_update_method());
				shutdown_screen_updating();
				return 0;
			}
			
			if(key[KEY_S] && showtext)
			{
				showtext = false;
			}
			else if (key[KEY_S])
				showtext = true;
			
			// process player input
         MyPlayer.GetControls();
			
			// pressing 1 switches update methods on the fly
			if(key[KEY_1] && !switchkey)
			{
				switchkey = true;
				initialize_screen_updating(get_update_method() + 1);
			}
			else if(!key[KEY_1])
				switchkey = false;
			
			// pressing 2 toggles vsync
			if(key[KEY_2] && !vsynckey)
			{
				vsynckey = true;
				toggle_vsync();
			}
			else if(!key[KEY_2])
				vsynckey = false;
			
			update_gfx = 1;
			--gametime;
		}
	}
}
END_OF_MAIN()
